package com.framework.web.bus.repository.specifications;

import com.framework.core.util.LikePatternUtil;
import com.framework.core.util.StringUtil;
import com.framework.web.bus.domain.Cookbook;
import com.framework.web.common.domain.BusinessConstants;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
* @Title: Specs
* @Description: 分页查询
* @author fusf
* @date 2016-09-04
* @version V1.0
*
*/
public class CookbookSpecs {

/**
* 分页查询
*
* @param  cookbook
* @return
*/
public static Specification<Cookbook> pageQueryCookbook(
final Cookbook  cookbook) {

return new Specification<Cookbook>() {
@Override
public Predicate toPredicate(Root<Cookbook> root,
CriteriaQuery<?> query, CriteriaBuilder builder) {
    Predicate predicate = builder.and();
    // equal codeCategory
/*
    if (StringUtil.isNotEmpty( cookbook.getCookbookType())) {
        predicate = builder.and(predicate, builder.like(
        builder.lower(root.<String>get(" cookbookType")),
        LikePatternUtil.getLikePattern( cookbook.getCookbookType())));
    }
  */
    if (StringUtil.isNotEmpty(cookbook.getCookbookName())) {
        predicate = builder.and(predicate, builder.like(
                builder.lower(root.<String>get("cookbookName")),
                LikePatternUtil.getLikePattern( cookbook.getCookbookName())));
    }

    //查询状态位为正常状态
    predicate = builder.and(predicate, builder.equal(
            root.<String>get("status"),
            BusinessConstants.STATUS.NORMAL));

        return predicate;
        }
    };
    }


    }
